<?php
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

function mod_jdirectory_menu_categories($id, $level, &$children,$itemid,$current_list,$expand,$order,$displaynumcontents,$directory) {
	global $cur_template;
	if (@$children[$id]) {
		foreach ($children[$id] as $row) {
			 if ($level == 0){
			    echo '<div>';
				$class="mainlevel";
			 }
			 else{
				echo '<div style="padding-left: '.($level*4).'px">';
				$class="sublevel";
			 }
             $itemid = getBossItemid($directory, $row->id);
			 $link = sefRelToAbs("index.php?option=com_boss&directory=$directory&task=show_category&catid=".$row->id."&order=$order&expand=$expand&Itemid=$itemid");
			 if ($displaynumcontents == 1)
			 {
				echo '<a href="'.$link.'" class="'.$class.'" >'.$row->name.' ('.$row->num_contents.')</a>';	
			 }
			 else
			 {
				echo '<a href="'.$link.'" class="'.$class.'" >'.$row->name.'</a>';
			 }
			 if (@$current_list[count($current_list) - 1 -$level] == $row->id)
			 {
				mod_jdirectory_menu_categories($row->id, $level+1, $children,$itemid,$current_list,$expand,$order,$displaynumcontents,$directory);
			 }
             echo '</div>';
		}
	}
}

function calc_nb_contents($id,&$children) {
		   $nb_contents = 0;
		   if (@$children[$id]) {
			  $nb = count($children[$id]);
			  for($i=0;$i < $nb;$i++)
			  {
				 $children[$id][$i]->num_contents += calc_nb_contents($children[$id][$i]->id,$children);
				 $nb_contents += $children[$id][$i]->num_contents;
			  }
		   }
		   return $nb_contents;
}

/****************************************************/
$catid = intval( mosGetParam( $_GET, 'catid', -1 ));
$expand = intval( mosGetParam( $_GET, 'expand', 0 ));
$order = intval( mosGetParam( $_GET, 'order', 0 ));
$directory = intval($params->get( 'directory', mosGetParam( $_GET, 'directory', 1 ) )) ;
$displaynumcontents = $params->def('displaynumcontents',1);
$sqltype = $params->def('sqltype',0);


$q = "SELECT c.*, ".
	"\n (select count(b.id) ".
	"\n  from #__boss_".$directory."_content_category_href as a, ".
    "\n  #__boss_".$directory."_contents as b ".
	"\n  where a.category_id = c.id ".
    "\n  and b.id = a.content_id ".
	"\n    and b.published = 1 ) ".
	"\n as num_contents ".
	"\n FROM #__boss_".$directory."_categories as c ".
	"\n WHERE c.published = 1 ".
	"\n ORDER BY c.parent,c.ordering";

$database->setQuery($q);						 
$rows = $database->loadObjectList();
if ($database -> getErrorNum()) {
	echo $database -> stderr();
	return false;
}

// establish the hierarchy of the menu
$children = array();
$orderlist = array();
// first pass - collect children
foreach ($rows as $v ) {
	$pt 	= $v->parent;
	$list 	= @$children[$pt] ? $children[$pt] : array();
	array_push( $list, $v );
	$children[$pt] = $list;
	$orderlist[$v->id] = $v;
}

if ($displaynumcontents == 1)
	$nb_contents = calc_nb_contents(0,$children,$sqltype);

$current_list[] = $catid;
if ($catid != -1)
{
	$current = $catid;
	while($orderlist[$current]->parent != 0)
	{
			$current_list[] = $orderlist[$current]->parent;
			$current = $orderlist[$current]->parent;
	}
}
require_once( JPATH_BASE .'/components/com_boss/lang/russian.php' );
require_once(JPATH_BASE.'/components/com_boss/boss.tools.php');

$itemid = getBossItemid($directory, 0);

$link_front = sefRelToAbs("index.php?option=com_boss&directory=$directory&Itemid=$itemid");
$link_write_content = sefRelToAbs("index.php?option=com_boss&directory=$directory&task=write_content&Itemid=$itemid");
$link_show_all = sefRelToAbs("index.php?option=com_boss&directory=$directory&task=show_all&order=$order&expand=$expand&Itemid=$itemid");
$link_my_contents = sefRelToAbs("index.php?option=com_boss&directory=$directory&task=show_user&Itemid=$itemid");
$link_show_profile = sefRelToAbs("index.php?option=com_boss&directory=$directory&task=show_profile&Itemid=$itemid");	
$link_show_rules = sefRelToAbs("index.php?option=com_boss&directory=$directory&task=show_rules&Itemid=$itemid");
?>
<div><a href="<?php echo $link_front; ?>" class="mainlevel" ><?php echo BOSS_MENU_HOME;?></a></div>
<div><a href="<?php echo $link_write_content; ?>" class="mainlevel" ><?php echo BOSS_MENU_WRITE;?></a></div>
<div><a href="<?php echo $link_show_profile; ?>" class="mainlevel" ><?php echo BOSS_MENU_PROFILE;?></a></div>
<div><a href="<?php echo $link_my_contents; ?>" class="mainlevel" ><?php echo BOSS_MENU_USER_CONTENTS;?></a></div>
<div><a href="<?php echo $link_show_rules; ?>" class="mainlevel" ><?php echo BOSS_MENU_RULES;?></a></div>
<div><span class="mainlevel" >- - - - - - -</span></div>
<?php
if ($displaynumcontents == 1)
	$all = BOSS_MENU_ALL_CONTENTS. "($nb_contents)";
else
	$all = BOSS_MENU_ALL_CONTENTS;
?>
<div><a href="<?php echo $link_show_all; ?>" class="mainlevel"><?php echo $all;?> </a></div>
<?php
mod_jdirectory_menu_categories(0, 0, $children,$itemid,$current_list,$expand,$order,$displaynumcontents,$directory);
?>
</table>